<script lang="ts" setup>
import { ref, onActivated } from 'vue';
import { $fetch } from '@/settings/utils/fetch';
import useNotification from '@/settings/hooks/notification';
import { useI18n } from 'vue-i18n';
import { dialog } from '@/settings/components/dialog';

const listing = ref([]);
const pager = ref({});
const loading = ref(false);
const search = ref();
const { success } = useNotification();
const { t } = useI18n();

// 标签分页表格
const refresh = async (page = 1) => {
  const { isSuccess, data } = await $fetch('/admin/label/page', {
    method: 'post',
    data: {
      pageNum: page,
      pageSize: 20,
    },
    loadingStatus: loading,
  });

  if (isSuccess) {
    listing.value = data.records;
    pager.value = {
      current: page,
      total: data.total,
    };
  }
}

// 删除标签
const remove = async (id: string) => {
  const { isSuccess } = await $fetch(`admin/label/delete/${id}`, {
    method: 'delete',
    data: {},
    loadingStatus: loading,
  });

  if (isSuccess) {
    success(t('settings.notification.del'));
    search.value.refresh();
  }
};

// 改变展示状态
const changeStatus = async (status: number, id: string) => {
  const { isSuccess } = await $fetch('/admin/label/open', {
    data: {
      id: id,
      status: status,
    },
    loadingStatus: loading,
  });
  if (isSuccess) {
    success(t('settings.notification.success'));
    search.value.refresh();
  }
};

// 打开弹窗新增标签
const add = async () => {
  const { button } = await dialog(() => import('./add-edit-window.vue'), {
    title: t('settings.view.tag.index.title1'),
  });

  if (button == 'ok') {
    search.value.refresh();
  }
}

// 打开弹窗编辑标签
const edit = async (id: string) => {
  const { button } = await dialog(() => import('./add-edit-window.vue'), {
    title: t('settings.view.tag.index.title2'),
    props: {
      id,
    },
  });

  if (button == 'ok') {
    search.value.refresh();
  }
};

onActivated(() => {
  search.value.refresh('reset');
});
</script>

<template>
  <OpenPageSection>
    <OpenSearchTable :pager="pager" :data="listing" :loading="loading" @search="refresh" ref="search">
      <template #header-button>
        <AButton @click="add" type="primary">{{ $t('settings.view.tag.index.button.add') }}</AButton>
      </template>

      <template #columns>
        <ATableColumn :title="$t('settings.view.tag.index.column1')" data-index="labelName" tooltip ellipsis />
        <ATableColumn :title="$t('settings.view.tag.index.column2')" :width="100" data-index="sort" />
        <ATableColumn :title="$t('settings.view.tag.index.column3')" :width="100">
          <template #cell="{ record }">
            <ASwitch v-model="record.labelState" size="small" :checked-value="1" :unchecked-value="0" @change="changeStatus(record.labelState, record.labelId)" />
          </template>
        </ATableColumn>

        <ATableColumn :title="$t('settings.view.tag.index.column4')" :width="180" data-index="createTime" />
        <ATableColumn :title="$t('settings.view.tag.index.column5')" :width="180">
          <template #cell="{ record }">
            <div class="button-actions">
              <AButton size="small" @click="edit(record.labelId)">{{ $t('settings.action.edit') }}</AButton>
              <AButton type="primary" status="danger" size="small" @click="remove(record.labelId)" v-confirm>{{ $t('settings.action.delete') }}</AButton>
            </div>
          </template>
        </ATableColumn>
      </template>
    </OpenSearchTable>
  </OpenPageSection>
</template>

<style lang="scss" scoped>
</style>
